package IO流.IO流操作.节点流.inputStream和outputStream.文件和文件夹拷贝;

import java.io.*;


/**
 * 使用IO技术将aaa.txt文件拷贝一份
 */
public class FIleCopy {
    public static void fileCopy() {
        File file = new File("src/IO流/IO流操作/输入流/aaa.txt");
        File copyFile = new File("src/IO流/IO流操作/输入流/bbb.txt");
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = new FileInputStream(file);
            outputStream = new FileOutputStream(copyFile);
            int temp;
            while ((temp = inputStream.read()) != -1) {
                outputStream.write(temp);
                outputStream.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != inputStream) {
                    inputStream.close();
                }
                if (null != outputStream) {
                    outputStream.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //使用字节数组复制文件
    public static void fileCopyByBytes(String filePath, String copyFilePath) {
        File file = new File(filePath);
        File copyFile = new File(copyFilePath);

        try (InputStream inputStream = new FileInputStream(file);
             OutputStream outputStream = new FileOutputStream(copyFile)) {


            byte[] bytes = new byte[20480];
            int temp;
            while ((temp = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes);
                outputStream.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    //使用字节缓冲流复制文件
    public static void fileCopyByBuffered(String filePath, String copyFilePath) {
        File file = new File(filePath);
        File copyFile = new File(copyFilePath);

        try (InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
             OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(copyFile))) {

            byte[] bytes = new byte[20480];
            int temp;
            while ((temp = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes);
                outputStream.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//21924
    public static void main(String[] args) {
        long t1 = System.currentTimeMillis();
        FIleCopy.fileCopyByBytes("F:\\迅雷下载\\招魂2.rmvb", "" +
                "C:\\huangzhi\\workspace\\javaStudy\\src\\IO流\\IO流操作\\节点流\\inputStream和outputStream\\文件和文件夹拷贝\\招魂4.rmvb");
        long t2 = System.currentTimeMillis();
        System.out.println(t2 - t1);
    }
}
